<template>
	<view class="wrap">
		<view>
			<view>
				<view class="header">
					<image :src="logoImg"></image>
				</view>
				<view class="content" style="min-height: 0;height:auto;margin-top: 30rpx;font-size: 28upx;">
					<view>我们的小程序将获取您的以下权限</view>
					<text style="font-size: 26upx;">获得你的公开信息(昵称，头像、地区及性别)</text>
				</view>
				<view class="caoz">
					<!-- #ifdef MP-TOUTIAO -->
					<button class="bottom" type="primary" withCredentials="true" lang="zh_CN" @click="DYGetUserInfo">授权登录</button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<button class="bottom" type="primary" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="login">授权登录</button>
					<!-- #endif -->
					<!-- #ifdef MP-QQ -->
					<button class="bottom" type="primary" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="GetUserInfo">授权登录</button>
					<!-- #endif -->
					<button class="bottom2" @click="quxiao()" style="border: none;">暂不登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var util = require('@/js/util.js');
	export default {
		data() {
			return {
				shop_index_top_image: '',
				logoImg: '',
				token:'',
				code:'',
				iv:'',
				encryptedData:''
			};
		},
		onLoad() {
			this.logoImg = this.$store.state.app_logo
		},
		methods: {
			open() { // 打开弹出层
				// this.showIslogin = true
			},
			quxiao() { // 返回
				uni.navigateBack({
					
				})
			},
			DYGetUserInfo() { // 字节小程序信息登陆
			let _self = this
				uni.login({ // 字节要先需要code
					provider: 'toutiao',
					success: function(loginRes) {
						console.log("login");
						console.log(loginRes.code);
						uni.getUserInfo({ // 获取信息
							success(userRes) {
								console.log("info");
								console.log(userRes)
								// 头像存储到本地
								uni.setStorageSync('headPortrait', userRes.userInfo.avatarUrl);
								uni.setStorageSync('userName', userRes.userInfo.nickName);
								// API请求
								uni.request({
									url: util.getUrl(),
									header: {
										'content-type': 'application/x-www-form-urlencoded',
									},
									method: 'POST',
									data: {
										controller: 'user.login',
										code: loginRes.code,
										nickname: userRes.userInfo.nickName,
										avatar: userRes.userInfo.avatarUrl,
										platform: 'toutiao',
										parent_id: uni.getStorageSync('parent_id') // 再分享小程序时设置id参数，被分享页面存储id，登陆时传。
									},
									success: res => { // 后端返回信息
										console.log(res)
										// 存储到本地
										uni.setStorageSync('token', res.data.data.token);

										// 存储到vuex公共数据
										_self.$store.commit('login', res.data.data.token)

										// 获取token
										console.log('token');
										console.log(uni.getStorageSync('token'));
										uni.navigateBack({}) // 返回上一页
										uni.request({ // 获取会员id 、用于邀请
											url: util.getUrl(),
											header: {
												'content-type': 'application/x-www-form-urlencoded',
											},
											method: 'POST',
											data: {
												controller: 'treasure.get_task_page',
												token: uni.getStorageSync('token'),
											},
											success: res => { // 返回邀请ID
												uni.setStorageSync('memberid', res.data.data.member_id);
												console.log('memberid');
												console.log(uni.getStorageSync('memberid'));
											}
										});
									}
								});
							}
						})
					}
				});
			},
			GetUserInfo() { // 微信QQ获取信息登陆
				let _this = this;
				uni.getUserInfo({
					// #ifdef MP-WEIXIN
					provider: 'weixin',
					// #endif
					// #ifdef MP-QQ
					provider: 'qq',
					// #endif
					success: function(infoRes) {
						console.log(infoRes);
						_this.encryptedData = infoRes.encryptedData
						_this.iv = infoRes.iv
						// 头像存储到本地
						uni.setStorageSync('avatar', infoRes.userInfo.avatarUrl);
						uni.setStorageSync('userName', infoRes.userInfo.nickName);
						_this.loginenter();
					},
					fail(res) {
						console.log(res);
					}
				});
			}, //登录
			login() {
				// uni.showLoading({
				// 	title: '登录中...'
				// });
				// 1.wx获取登录用户code
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						_this.code = loginRes.code
						uni.showModal({
							title:"提示",
							content:"获取用户头像，昵称等信息",
							success: (res) => {
								if(res.confirm){
									wx.getUserProfile({
										desc:'用于展示用户头像，昵称等信息！',
										success: function(res) {
											console.log(res);
											_this.loginenter(res.userInfo.avatarUrl,res.userInfo.nickName,res.encryptedData,res.iv,res.userInfo.gender)
										},
										fail(res) {
											console.log(res);
										}
									});
								}
							}
						})
					}
				})
			},
			loginenter(...data){
				console.log(data)
				uni.request({
					url: util.getUrl("auth/login"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					method: 'POST',
					data: {
						code: this.code,
<<<<<<< HEAD
						iv:data[3],
						nickName:data[1],
						avatarUrl:data[0],
						encryptedData:data[2],
						parent_id: uni.getStorageSync('parent_id'), // 再分享小程序时设置id参数，被分享页面存储id，登陆时传。
						gender:data[4]
=======
						iv:this.iv,
						encryptedData:this.encryptedData,
						parent_id: uni.getStorageSync('parent_id'), // 再分享小程序时设置id参数，被分享页面存储id，登陆时传。
						prizedraw_id: uni.getStorageSync('prizedraw_id')
>>>>>>> 263842a02420c7090936b7737331a37d9618b572
					},
					success: res => {
						uni.hideLoading()
						// 存储到本地
						this.$store.commit("changeIsNewUser",res.data.data.is_new)
						uni.setStorageSync('token', res.data.data.token);
						uni.setStorageSync('member_id', res.data.data.member_id);
						uni.setStorageSync("is_new",res.data.data.is_new)
						console.log(uni.getStorageSync('member_id'));
						if(uni.getStorageSync('parent_id')){
							uni.reLaunch({
								url:'../../synthesis/other/index'
							})
						}else{
							uni.navigateBack({
								
							})
						}
					}
				});
			}
		}
	};
</script>

<style>
	.caoz {
		width: 100%;
		padding: 0 30upx;
		box-sizing: border-box;
		margin-top: 50upx;
	}

	.wrap {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background: #fff;
		z-index: 200;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.content {
		text-align: center;
	}

	.header {
		text-align: center;
		justify-content: center;
		box-sizing: border-box;
	}

	.header image {
		width: 400upx;
		height: 400upx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 10rpx;
	}

	.bottom2 {
		border-radius: 80rpx;
		margin: 40rpx auto;
		font-size: 28rpx;
		color: #fff;
		width: 300upx;
		height: 80upx;
		border-radius: 80upx;
		background: #d7d7d5;
	}

	.bottom2:after {
		border-radius: 100upx;
		border: none;
		font-size: 68rpx;
	}

	.bottom {
		margin: 30rpx auto;
		font-size: 28rpx;
		color: #fff;
		width: 300upx;
		height: 80upx;
		border-radius: 80upx;
		background: rgb(251,85,89);
	}

	.bottom::after {
		content: '授权登录';
		width: 200%;
		height: 200%;
		background: rgb(251,85,89);
		color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 68rpx;
		border: none;
	}
</style>
